part of '../repository.dart';

const _kPurchaseTransactionOwnershipTypeValue = 'purchase';
const _kFamilySharingTransactionOwnershipTypeValue = 'family_sharing';

class _TransactionOwnershipTypeDto extends Equatable {
  const _TransactionOwnershipTypeDto._internal({
    required this.ownershipTypeString,
  });

  final String ownershipTypeString;

  @override
  List<Object?> get props {
    return [
      ownershipTypeString,
    ];
  }

  factory _TransactionOwnershipTypeDto.fromString(String ownershipTypeString) {
    return _TransactionOwnershipTypeDto._internal(
      ownershipTypeString: ownershipTypeString,
    );
  }

  factory _TransactionOwnershipTypeDto.fromEntity(AppStoreTransactionOwnershipType ownershipType) {
    late final String ownershipTypeString;

    if (ownershipType == AppStoreTransactionOwnershipType.purchase) {
      ownershipTypeString = _kPurchaseTransactionOwnershipTypeValue;
    } else if (ownershipType == AppStoreTransactionOwnershipType.familySharing) {
      ownershipTypeString = _kFamilySharingTransactionOwnershipTypeValue;
    }

    return _TransactionOwnershipTypeDto._internal(
      ownershipTypeString: ownershipTypeString,
    );
  }

  @override
  String toString() {
    return ownershipTypeString;
  }

  AppStoreTransactionOwnershipType toEntity() {
    if (ownershipTypeString == _kPurchaseTransactionOwnershipTypeValue) {
      return AppStoreTransactionOwnershipType.purchase;
    }

    if (ownershipTypeString == _kFamilySharingTransactionOwnershipTypeValue) {
      return AppStoreTransactionOwnershipType.familySharing;
    }

    throw UnimplementedError();
  }
}
